package com.ac.scum.log.web.model.player.mapper;

import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.PlayerVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface PlayerMapper extends BaseMapper<Player> {


    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p left join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and p.steam_id in (${steamIds})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayersByServerIdAndSteamIdStr(@Param("serverId") Long serverId, @Param("steamIds") String steamIds);

    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time," +
            "pb.balance,pb.total_balance,pb.last_recharge_time, " +
            "wmu.open_id,wmu.nickname,wmu.avatar as wechat_avatar,wmu.sex,wmu.is_subscribe,wmu.subscribe_time " +
            "from player p " +
            "left join server_player pjs on pjs.player_steam_id = p.steam_id " +
            "left join player_balance pb on pb.player_steam_id = p.steam_id " +
            "left join wechat_user_player wup on wup.steam_id = p.steam_id " +
            "left join wechat_mp_user wmu on wup.open_id = wmu.open_id " +
            "where pjs.server_id = #{serverId} and ${where} order by pjs.join_time desc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),

            @Result(property = "balance", column = "balance"),
            @Result(property = "totalBalance", column = "total_balance"),
            @Result(property = "lastRechargeTime", column = "last_recharge_time"),
            @Result(property = "createTime", column = "create_time"),

            @Result(property = "openId", column = "open_id"),
            @Result(property = "wechatNickname", column = "nickname"),
            @Result(property = "wechatAvatar", column = "wechat_avatar"),
            @Result(property = "wechatSex", column = "sex"),
            @Result(property = "isSubscribe", column = "is_subscribe"),
            @Result(property = "subscribeTime", column = "subscribe_time"),

    })
    IPage<PlayerVO> findPlayerPageByWhereSql(@Param("serverId") Long serverId, @Param("where") String where, Page<?> page);


    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p left join server_player pjs on pjs.player_steam_id = p.steam_id where  p.steam_id in (${steamIds})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayersBySteamIdStr(@Param("steamIds") String steamIds);

    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p inner join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and p.id in (${ids})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayersByServerIdAndIdsStr(@Param("serverId") Long serverId, @Param("ids") String ids);

    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p inner join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and p.id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    Player findPlayerByServerIdAndId(@Param("serverId") Long serverId, @Param("id") Long id);

    default List<Player> findPlayerByServerIdAndSteamIds(Long serverId, List<Long> steamIds) {
        if (steamIds.isEmpty()) {
            return Lists.newArrayList();
        }
        return findPlayersByServerIdAndSteamIdStr(serverId, steamIds.stream().map(Object::toString).collect(Collectors.joining(",")));
    }

    default List<Player> findPlayerBySteamIds(List<Long> steamIds) {
        if (steamIds.isEmpty()) {
            return Lists.newArrayList();
        }
        return findPlayersBySteamIdStr(steamIds.stream().map(Object::toString).collect(Collectors.joining(",")));
    }

    default List<Player> findPlayerByServerIdAndIds(Long serverId, List<Long> ids) {
        if (ids.isEmpty()) {
            return Lists.newArrayList();
        }
        return findPlayersByServerIdAndIdsStr(serverId, ids.stream().map(Object::toString).collect(Collectors.joining(",")));
    }

    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p left join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and (pjs.name like '%${playerName}%' or pjs.fake_name like '%${playerName}%' and ${where})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayersByServerIdAndNameLike(@Param("serverId") Long serverId, @Param("playerName") String playerName, @Param("where") String where);

    @Select("select count(*) from server_player where server_id = #{serverId} and join_time>#{startTime} and join_time < #{endTime}")
    Integer findNewPlayerCountByDate(@Param("serverId") Long serverId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    @Select("select p.*,pjs.server_id,pjs.fake_name,pjs.name,pjs.join_time from player p left join server_player pjs on pjs.player_steam_id = p.steam_id where pjs.server_id = #{serverId} and join_time > #{startTime} and join_time < #{endTime}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "steamId", column = "steam_id"),
            @Result(property = "serverId", column = "server_id"),
            @Result(property = "fakeName", column = "fake_name"),
            @Result(property = "joinTime", column = "join_time"),
            @Result(property = "steamName", column = "steam_name"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Player> findPlayersByDate(@Param("serverId") Long serverId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

}
